package com.njym.java.security.app.rest;

import com.github.pagehelper.PageInfo;
import com.njym.java.security.app.biz.LikeOrCollectBiz;
import com.njym.java.security.app.entity.LikeOrCollect;
import com.njym.java.security.app.entity.LikeOrCollectVo;
import com.njym.java.security.common.msg.ObjectRestResponse;
import com.njym.java.security.common.msg.TableResultResponse;
import com.njym.java.security.common.rest.BaseController;
import com.njym.java.security.common.util.ResponseUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("likeOrCollect")
public class LikeOrCollectController extends BaseController<LikeOrCollectBiz,LikeOrCollect> {

    /**
     * 进入课程页面，获取用户是否点赞或收藏
     * @param couId
     * @param userId
     * @return
     */
    @RequestMapping(value = "/getCourseLikeOrCollect", method = RequestMethod.GET)
    @ResponseBody
    public Object getCourseLikeOrCollect(String couId, String userId){
        if (!couId.equals("undefined") && !userId.equals("undefined")) {
            LikeOrCollect likes = new LikeOrCollect();
            likes.setType(2);
            likes.setUserId(Integer.parseInt(userId));
            likes.setCouId(Integer.parseInt(couId));


            LikeOrCollect collects = new LikeOrCollect();
            collects.setType(1);
            collects.setUserId(Integer.parseInt(userId));
            collects.setCouId(Integer.parseInt(couId));

            Map<String,Object> data  = new HashMap<>();
            Long like = baseBiz.selectCount(likes);
            Long collect = baseBiz.selectCount(collects);
            data.put("like",like);
            data.put("collect",collect);
            return new ObjectRestResponse<>().rel(true).data(data);

        }else {    //如果参数为undefind
            return ResponseUtil.fail(40301, "错误！");
        }
    }

    /**
     * 1、收藏   2、点赞
     */
    @RequestMapping(value = "/like_or_collect", method = RequestMethod.GET)
    @ResponseBody
    public Object like_or_collect(Integer type, String couId, String userId) {
        if (!couId.equals("undefined") && !userId.equals("undefined")) {
            LikeOrCollect entity = new LikeOrCollect();
            entity.setCouId(Integer.parseInt(couId));
            entity.setUserId(Integer.parseInt(userId));
            entity.setModdifyTime(new Date());
            if(type == 1){
                entity.setType(type);
                baseBiz.insertSelective(entity);
                return new ObjectRestResponse<>().rel(true).data(1);
            }else {
                entity.setType(type);
                baseBiz.insertSelective(entity);
                return new ObjectRestResponse<>().rel(true).data(2);
            }
        }else {    //如果参数为undefind
            return new ObjectRestResponse<>().rel(true).data(3);
        }
    }


    /**
     *
     * @param couId
     * @param userId
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.GET)
    @ResponseBody
    public Object del(Integer type,String couId, String userId){
        if (!couId.equals("undefined") && !userId.equals("undefined")) {
            List<LikeOrCollect> list = baseBiz.getList(type,Integer.parseInt(couId),Integer.parseInt(userId));
            if(type == 1){
                if(list.size() > 0){
                    baseBiz.delByParam(type,Integer.parseInt(couId),Integer.parseInt(userId));
                    return new ObjectRestResponse<>().rel(true).data(1);
                } else {
                    return new ObjectRestResponse<>().rel(true).data(2);
                }
            } else {
                if(list.size() > 0){
                    baseBiz.delByParam(type,Integer.parseInt(couId),Integer.parseInt(userId));
                    return new ObjectRestResponse<>().rel(true).data(3);
                }else {
                    return new ObjectRestResponse<>().rel(true).data(4);
                }
            }
        }else {    //如果参数为undefind
            return new ObjectRestResponse<>().rel(true).data(5);
        }
    }

    /**
     * 我的收藏
     * @param limit
     * @param page
     * @param userId
     * @return
     */
    @RequestMapping(value = "/getmycollect", method = RequestMethod.GET)
    @ResponseBody
    public Object getCourseLikeOrCollect(@RequestParam(defaultValue = "10") int limit, @RequestParam(defaultValue = "1") int page,String userId){
        if (!userId.equals("undefined")) {
            List<LikeOrCollectVo> list = baseBiz.getMyCollect(Integer.parseInt(userId));
            long total = PageInfo.of(list).getTotal();
            int  totalPages= (int)Math.ceil((double)total/limit);
            Map<String, Object> data = new HashMap<>(2);
            data.put("list",list);
            data.put("totalPages",totalPages);
            return new ObjectRestResponse<LikeOrCollectVo>().rel(true).data(data);
        }else {
            return ResponseUtil.fail(40301, "错误！");
        }
    }

}